package predictionEngine;

import com.orsoncharts.table.VerticalFlowElement;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import java.awt.GridLayout;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

import javax.swing.JPanel;

import junit.extensions.TestSetup;

import org.jfree.chart.ChartPanel;
import org.jfree.ui.RefineryUtilities;

public class Starter {
    public static void main(String[] args) {
        Starter starter = new Starter();   
        double testPercent = 0.15;
       ArffReader ar = new ArffReader("Streetfriends7-NoStamp.arff");
        //ArffReader ar = new ArffReader("reg-auto_price.arff");
       //ArffReader ar = new ArffReader("reg-machine_cpu.arff");
       /* JFileChooser fc = new JFileChooser();
        fc.showOpenDialog(null);
        ArffReader ar = new ArffReader(fc.getSelectedFile().getAbsoluteFile().toString());;//*/
        ar.loadData();
        Dataset dataset = new Dataset(ar.getInstances(),ar.getAttributes());
        Dataset[] sets = dataset.split(testPercent);
        dataset = sets[0];//just the training data

        
        ar.loadData();
        Dataset dataset2 = new Dataset(ar.getInstances(),ar.getAttributes());
        sets = dataset2.split(testPercent);
        
        TreeNode root=null;
        //for(int i=0;i<1000;i++)
        root=new TreeNode(null,dataset);
        
        RandomForest forest = new RandomForest(1000);
        forest.createForest(dataset,true);
        
        //System.out.println(root);      
       // System.out.println("Actual\tPrediction");  
        double MAE=0;
        for(double[] instance:dataset.getInstances()){
            System.out.println(instance[instance.length-1]+"\t"+forest.execute(instance));      
            MAE+=Math.abs(instance[instance.length-1]-forest.execute(instance));
        }
       // System.out.println(root);
        System.out.println("\nMAE:"+MAE/dataset.size()); 
        ar=null;
        
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(2,1));
        ChartPanel chart = LineChartFactory.getInstance().makePredictionChart(sets[0],sets[1], forest);
        ChartPanel chart2 = LineChartFactory.getInstance().makeAttEvalChart(dataset2, forest,5);
;
       // frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(chart);
        frame.getContentPane().add(chart2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setExtendedState( frame.getExtendedState()|JFrame.MAXIMIZED_BOTH );        
        frame.setVisible(true);
       // RefineryUtilities.centerFrameOnScreen(frame);
        
        //ar.readArffFile("reg-auto_price.arff");
    }
}
